﻿using Microsoft.Extensions.Logging;
using PropertyManagment.config;
using PropertyManagment.entity;
using PropertyManagment.entity.dto;
using PropertyManagment.mapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.service.impl {
    public class ServiceImpl<M,T> : IService<T> where T : BaseEntity where M : BaseMapper<T>{

        public M baseMapper { get; set; }

        public List<T> getAll() {
            return baseMapper.getAll().ToList();
        }

        public T getById(long id) {
            return baseMapper.getById(id);
        }
        public List<T> listByIds(ICollection<long> idList) {
            return baseMapper.selectBatchIds(idList).ToList();
        }

        public T add(T sysUser) {
            return baseMapper.add(sysUser);
        }

        public List<T> addList(ICollection<T> dataList) {
            return baseMapper.addBatch(dataList).ToList();
        }

        public T delete(long id) {
            return baseMapper.delete(id);
        }
        public List<T> deleteList(ICollection<T> dataList) {
            return baseMapper.deleteBatch(dataList).ToList();
        }

        public List<T> deleteByIds(ICollection<long> idList) {
            return baseMapper.deleteBatchIds(idList).ToList();
        }

        public T update(T updateUser) {
            return baseMapper.update(updateUser);
        }

        public Page<T> page(Page<T> page) {
            return baseMapper.page(page);
        }

        public Page<T> page(Page<T> page, ICollection<T> list) {
            int num = (page.current - 1) * page.size;
            if (num >= list.Count()) {
                num = ((int)Math.Ceiling((double)list.Count() / page.size) - 1) * page.size;
            }
            List<T> ls = list.Skip(num).Take(page.size).ToList();
            page.records = ls;
            page.total = list.Count();
            return page;
        }
    }
}
